<template>
  <div class="app">
    <el-card style="width: 480px">
      <template #header>
        <div class="card-header">
          <h1>登录</h1>
        </div>
      </template>

      <!-- 登录表单 -->

      <!-- :rules="rules" 整个表单的校验规则 -->
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <!-- prop="name" --采用的 name属性的校验规则-->
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            登录
          </el-button>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>

      <template #footer>Footer content</template>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";

import type { ComponentSize, FormInstance, FormRules } from "element-plus";
import { login } from "@/api/login";
import router from "@/router";
import { ElMessage } from "element-plus";

// 接口--RuleForm 控制表单中有哪些属性
interface RuleForm {
  name: string;
  password: string;
}

const formSize = ref<ComponentSize>("default");

// 组件实例
const ruleFormRef = ref<FormInstance>();
// 响应式数据
const ruleForm = reactive<RuleForm>({
  name: "",
  password: "",
});

//个表单的校验规则
// FormRules限制是的验证规则可以有哪些属性
const rules = reactive<FormRules<RuleForm>>({
  // name属性的校验规则

  password: [
    {
      pattern: /^.{5,}$/,
      message: "密码至少5为非空字符",
      required: true,
    },
  ],
  name: [
    //  required: true, message: '请输入用户名', trigger: 'blur'
    // 是否必须           失败提示                  触发条件
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 35, message: "Length should be 3 to 5", trigger: "blur" },
  ],
});

// formEl -form表单的组件实例
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;

  //validate对整个表单的内容进行验证。 接收一个回调函数，或返回 Promise。
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      console.log("submit!");
      //校验通过 -》调用登录接口--》登录成功进行跳转
      // loginReq
      const r = await login(ruleForm.name, ruleForm.password);
      console.log(r.data);
      // r.data.data.adminname
      // r.data.code
      const data = r.data;
      if (data.code == "200") {
        console.log("1");

        ElMessage({
          message: data.message,
          type: "success",
          plain: true,
        });
        //登录成功存入token和adminname到本地
        localStorage.setItem("xx-mall-token", data.data.token);
        localStorage.setItem("xx-mall-adminname", data.data.adminname);

        router.push("/home");
      } else {
        ElMessage({
          message: data.message,
          type: "error",
          plain: true,
        });
      }
    } else {
      // fields --校验不通过 规则对象
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style scoped lang="scss">
.app {
  background-image: url("../assets/images/bg.jpg");
  height: 100%;
  display: flex;
  .el-card {
    margin: auto;
  }
}
</style>
